package com.future.mayi.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.future.baselib.utils.ToastUtils;
import com.future.baselib.utils.statusbarlibrary.StatusBarUtils;
import com.future.baselib.view.LoadingDialog;
import com.future.mayi.R;
import com.future.mayi.utils.HCUtils;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import butterknife.ButterKnife;


/**
 * Created by 周大虎 on 2016/12/1.
 */

public abstract class BaseActivity extends FragmentActivity {
    protected Context mContext;

    protected ToastUtils toast;
    private LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtils.setStatusBarStyle(getWindow(), true);
        mContext = this;
        toast = new ToastUtils(this);
        //加载布局
        initLayout();
        //加载View
        initView();
        //加载数据
        initDates();
        //设置监听
        initListener();
    }


    protected TextView findTextView(int id) {
        return (TextView) findViewById(id);
    }

    protected ImageView findImageView(int id) {
        return (ImageView) findViewById(id);
    }

    protected LinearLayout findLinearLayout(int id) {
        return (LinearLayout) findViewById(id);
    }

    protected RelativeLayout findRelativeLayout(int id) {
        return (RelativeLayout) findViewById(id);
    }

    protected void toast(String message) {
        ToastUtils.shortToast(this, message);
    }

    protected abstract void initLayout();

    protected abstract void initView();

    protected abstract void initDates();

    protected abstract void initListener();


    /**
     * 加载对话框
     */
    public void showLoadingDialog() {
        if (dialog == null) {
            dialog = new LoadingDialog(this);
        }
        dialog.show();
    }

    public void dissmissLoadingDialog() {
        if (dialog != null) {
            dialog.close();
        }
    }

    public void startActivity(Class activityClass) {
        startActivity(activityClass, null);
    }

    public void startActivity(Class activityClass, Bundle bundle) {
        Intent intent = new Intent(this, activityClass);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
        ButterKnife.bind(this);
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        if (layoutResID != R.layout.activity_layout_base)
            ButterKnife.bind(this);
    }


    protected void setText(TextView textView, CharSequence content) {
        if (textView == null)
            return;
        textView.setText(TextUtils.isEmpty(content) ? "" : content);
    }

    protected void setText(TextView textView, int resourceId) {
        if (textView == null)
            return;
        textView.setText(mContext.getResources().getString(resourceId));
    }

    protected void loadImg(ImageView imageView, String imgUrl) {
        HCUtils.loadWebImg(mContext, imageView, imgUrl);
    }

    protected void loadImg(ImageView imageView, int resId) {
        HCUtils.loadWebImg(mContext, imageView, resId);
    }

    protected Activity getActivity() {
        return this;
    }


    protected void setText(TextView textView, String content) {
        if (textView == null) return;
        textView.setText(content == null ? "" : content );
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
    }
}
